package com.fmdj.snm.controller;

import com.fmdj.common.util.CommonResult;
import com.fmdj.snm.controller.form.ClearNewOrderQueueForm;
import com.fmdj.snm.controller.form.DeleteNewOrderQueueForm;
import com.fmdj.snm.controller.form.ReceiveNewOrderMessageForm;
import com.fmdj.snm.controller.form.SendNewOrderMessageForm;
import com.fmdj.snm.entry.NewOrderMessage;
import com.fmdj.snm.task.NewOrderMassageTask;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.math.Primes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/message/order/new")
@Tag(name = "NewOrderMessageController", description = "新订单消息Web接口")
@Slf4j
public class NewOrderMessageController {

    @Autowired
    private NewOrderMassageTask newOrderMassageTask;

    @PostMapping("/sendNewOrderMessage")
    @Operation(summary = "同步发送新订单消息")
    public CommonResult sendNewOrderMessage(@Valid @RequestBody SendNewOrderMessageForm form) {
        ArrayList<NewOrderMessage> list = new ArrayList<>();

        //遍历司机的信息创建一条条新订单消息
        for (@NotEmpty(message = "driversContent不能为空") String one : form.getDriversContent()) {
            NewOrderMessage message = new NewOrderMessage();
            String[] temp = one.split("#");
            String userId = temp[0]; //司机id
            message.setUserId(userId);
            message.setOrderId(String.valueOf(form.getOrderId()));
            message.setExpectsFee(form.getExpectsFee());
            message.setMileage(form.getMileage());
            message.setTo(form.getTo());
            message.setMinute(String.valueOf(form.getMinute()));
            String distance = temp[1];
            message.setDistance(distance);
            message.setFavourFee(form.getFavourFee());
            message.setFrom(form.getFrom());

            list.add(message);
        }

        newOrderMassageTask.sendNewOrderMessage(list);
        return CommonResult.ok();
    }

    @PostMapping("/sendNewOrderMessageAsync")
    @Operation(summary = "异步发送新订单消息")
    public CommonResult sendNewOrderMessageAsync(@Valid @RequestBody SendNewOrderMessageForm form) {
        ArrayList<NewOrderMessage> list = new ArrayList<>();

        //遍历司机的信息创建一条条新订单消息
        for (@NotEmpty(message = "driversContent不能为空") String one : form.getDriversContent()) {
            NewOrderMessage message = new NewOrderMessage();
            String[] temp = one.split("#");
            String userId = temp[0]; //司机id
            message.setUserId(userId);
            message.setOrderId(String.valueOf(form.getOrderId()));
            message.setExpectsFee(form.getExpectsFee());
            message.setMileage(form.getMileage());
            message.setTo(form.getTo());
            message.setMinute(String.valueOf(form.getMinute()));
            String distance = temp[1]; //司机接单距离
            message.setDistance(distance);
            message.setFavourFee(form.getFavourFee());
        }

        newOrderMassageTask.sendNewOrderMessageAsync(list);
        return CommonResult.ok();
    }

    @PostMapping("/receiveNewOrderMessage")
    @Operation(summary = "接受新订单消息")
    public CommonResult receiveNewOrderMessage(@Valid @RequestBody ReceiveNewOrderMessageForm form) {
        List<NewOrderMessage> list = newOrderMassageTask.receiveNewOrderMessage(form.getUserId());
        return CommonResult.ok().put(CommonResult.RETURN_RESULT,list);
    }

    @PostMapping("/deleteNewOrderQueue")
    @Operation(summary = "同步删除新订单消息队列")
    public CommonResult deleteNewOrderQueue(@RequestBody @Valid DeleteNewOrderQueueForm form) {
        newOrderMassageTask.deleteNewOrderQueue(form.getUserId());
        return CommonResult.ok();
    }

    @PostMapping("/deleteNewOrderQueueeAsync")
    @Operation(summary = "异步删除新订单消息队列")
    public CommonResult deleteNewOrderQueueeAsync(@RequestBody @Valid DeleteNewOrderQueueForm form) {
        newOrderMassageTask.deleteNewOrderQueueAsync(form.getUserId());
        return CommonResult.ok();
    }

    @PostMapping("/clearNewOrderQueue")
    @Operation(summary = "同步清空新订单消息队列")
    public CommonResult clearNewOrderQueue(@RequestBody @Valid ClearNewOrderQueueForm form) {
        newOrderMassageTask.clearNewOrderQueue(form.getUserId());
        return CommonResult.ok();
    }

    @PostMapping("/clearNewOrderQueueAsync")
    @Operation(summary = "异步清空新订单消息队列")
    public CommonResult clearNewOrderQueueAsync(@RequestBody @Valid ClearNewOrderQueueForm form) {
        newOrderMassageTask.clearNewOrderQueueAsync(form.getUserId());
        return CommonResult.ok();
    }
}
